/*  
  -- The Lime II Project -- 

  A tuplespaces-based middleware for coordinating agents and mobile hosts.
  Copyright (C) 2005.
  Gian Pietro Picco, Amy L. Murphy, Lorenzo Bellini.

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  Lesser General Public License for more details.

  You should have received this copy of the GNU Lesser General Public
  License along with this library; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 
 */

package testing;

import lights.adapters.Field;
import lights.adapters.Tuple;
import lights.interfaces.ITuple;
import lime2.Location;
import sagroup.dracots.DracoTS;
import sagroup.dracots.DracoTuple;
import sagroup.dracots.FlatShareTuple;
import sagroup.dracots.ShareTuple;
import sagroup.dracots.TupleType;

/**
 * A dome agent performing some basic operations. Users can 
 * consider this code as a simple tutorial to the tuple space.<br>
 * 
 * @author Huang Xiaodong
 */

public class DemoAgent implements Runnable {
	/**
	 * Constructor method (default). Destination location is here.
	 */
	public DemoAgent() {
	}

	/**
	 * Run method.
	 */
	public void run() {
		System.out.println("MyAgent started");
		try {
			// Here to get a DracoTupleSpace instance named testing.
			DracoTS dts = DracoTS.get("testing");
			System.out.println("Get DracoTS.");

			// Turn on the monitor to keep record of operations
			dts.setMonitorStatus(false);
			System.out.println("Turn on monitor.");

			// Out a new tuple <hello, world>
			ITuple tuple1 = new Tuple().add(new Field().setValue("hello"));
			tuple1.add(new Field().setValue("world"));
			System.out.println("Out performed out of "
					+ dts.out(new FlatShareTuple(new TupleType("type1"), 
							Location.HERE, tuple1)));

			// Out a new tuple <lime, tuple, space, manager>
			ITuple tuple2 = new Tuple().add(new Field().setValue("lime"));
			tuple2.add(new Field().setValue("tuple"));
			tuple2.add(new Field().setValue("space"));
			tuple2.add(new Field().setValue("manager"));
			System.out.println("Out performed out of "
					+ dts.out(new FlatShareTuple(new TupleType("type2"), 
							Location.HERE, tuple2)));

			// Out a new tuple <software, computer, architecture>
			ITuple tuple3 = new Tuple().add(new Field().setValue("software"));
			tuple3.add(new Field().setValue("computer"));
			tuple3.add(new Field().setValue("architecture"));
			System.out.println("Out performed out of "
					+ dts.out(new FlatShareTuple(new TupleType("type3"), 
							Location.HERE, tuple3)));

			ShareTuple st = ShareTuple
					.createTypeTemplate(new TupleType("type3"));
			DracoTuple dt = dts.rdp(st);
			System.out.println("Rdp performed of " + dt);
			System.out.println("MyAgent done!");

			// output the log file and clear the log cache
			// You can use the first parameter of function outputLog to set
			// output path.
			String fullPath = dts.outputLog(null, true);
			System.out.println("Finish writing log file " + fullPath);
		} catch (Exception e) {
			System.out.println("Operation failed!");
			e.printStackTrace();
		}
	}
}